HTMLify
525. Contiguous Array.java
Views: 1 | Author: cody
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | // 525. Contiguous Array java solution import java.util.HashMap; class Solution { public int findMaxLength(int[] arr) { int pc0 = 0; int pc1 = 0; HashMap<Integer, Integer> map = new HashMap<>(); map.put(0, -1); int maxLength = 0; for (int ep = 0; ep < arr.length; ep++) { if (arr[ep] == 0) pc0++; else pc1++; int diff = pc1 - pc0; if (map.containsKey(diff)) { maxLength = Math.max(maxLength, ep - map.get(diff)); } else { map.put(diff, ep); } } return maxLength; } } |